import { t } from './lang'
import * as Enums from '@/enums'

const enumMap = new Map()

Object.entries(Enums).forEach(([key, val]) => {
	enumMap.set(val, key)
})

/**
 * 获取枚举列表
 * @param {*} enumObj
 * @returns
 */
export function enumList(enumObj: any) {
	if (!enumObj) {
		return []
	}
	return Object.entries(enumObj)
		.filter((key: any) => isNaN(Number(enumObj[key])))
		.map(([key, val]) => ({
			id: val as string | number,
			textT: enumMap.get(enumObj) + '.' + key,
			text: t(enumMap.get(enumObj) + '.' + key)
		}))
}

/**
 * 获取枚举名称
 * @param {*} enumObj
 * @param {*} enumId
 * @returns
 */
export function enumName(enumObj: any, enumId: any) {
	for (const [key, val] of Object.entries(enumObj)) {
		if (val === enumId) {
			return t(enumMap.get(enumObj) + '.' + key)
		}
	}
}

export function enumKey(enumObj: any, enumVal: any): string {
	for (const [key, val] of Object.entries(enumObj)) {
		if (val === enumVal) {
			return key
		}
	}
	return ''
}

export function enumValue(enumObj: any, enumKey: string): any {
	for (const [key, val] of Object.entries(enumObj)) {
		if (key === enumKey) {
			return val
		}
	}
	return ''
}

export function enumValue2(
	enumObj: object,
	enumKey: string,
	type: string = 'id',
	keyValue: string = 'name'
) {
	/* eslint-disable @typescript-eslint/no-unused-vars */
	return Object.entries(enumObj).find(([_, val]: [any, any]) => {
		return Number(enumKey) === Number(val[type])
	})?.[1][keyValue]
	/* eslint-enable @typescript-eslint/no-unused-vars */
}

export function isEnum(enumObj: any, enumVal: any): boolean {
	return Object.values(enumObj).includes(enumVal)
}
